package com.hunttown.mes.manage.controller.common;

import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.domain.TransportUserDTO;
import com.hunttown.mes.manage.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: 模糊查询工具类
 * @author: wangjunfu
 * @date: 2020年12月10日 8:48
 * @description:
 */
@Controller
@RequestMapping(value = "/get-similar")
public class GetSimilarController {
    private final static Logger logger = LoggerFactory.getLogger(GetSimilarController.class);

    private final AnalysisCatalogManageService catalogService;
    private final AnalysisManageManageService manageService;
    private final TransportCompanyManageService companyService;
    private final TransportPurchaseManageService purchaseService;
    private final TransportUserManageService userService;

    @Autowired
    public GetSimilarController(AnalysisCatalogManageService catalogService, AnalysisManageManageService manageService, TransportCompanyManageService companyService, TransportPurchaseManageService purchaseService, TransportUserManageService userService){
        this.catalogService = catalogService;
        this.manageService = manageService;
        this.companyService = companyService;
        this.purchaseService = purchaseService;
        this.userService = userService;
    }

    //region AJAX 动态获取数据库中【管理员】信息

    /**
     * AJAX 动态获取数据库中管理员信息
     *
     * @param filter 搜索字段
     * @return 相似项
     */
    @RequestMapping(value = "/getSimilarManage", method = RequestMethod.POST)
    @ResponseBody
    public Map<Object, Object> getSimilarManage(String filter) {

        Map<Object, Object> map = new HashMap<>();

        filter = filter.trim();

        if (StringUtils.isNotBlank(filter)) {
            List<Map<String, String>> dataList = manageService.getSimilarManage(filter);
            for (Map<String, String> child : dataList) {
                map.put(child.get("id"), child.get("similar"));
            }
        }

        if (map.size() == 0) {
            map.put(0, "未查询到数据");
        }

        return map;
    }

    //endregion

    //region AJAX 动态获取数据库中【分类】信息

    /**
     * AJAX 动态获取数据库中分类信息
     *
     * @param filter 搜索字段
     * @return 相似项
     */
    @RequestMapping(value = "/getSimilarCatalog", method = RequestMethod.POST)
    @ResponseBody
    public Map<Object, Object> getSimilarCatalog(String filter, Integer modelType) {

        Map<Object, Object> map = new HashMap<>();

        filter = filter.trim();

        if (StringUtils.isNotBlank(filter)) {
            List<Map<String, String>> dataList = catalogService.getSimilarCatalog(filter, modelType);
            for (Map<String, String> child : dataList) {
                map.put(child.get("id"), child.get("similar"));
            }
        }

        if (map.size() == 0) {
            map.put("0", "未查询到数据");
        }

        return map;
    }

    //endregion

    //region AJAX 动态获取数据库中【开过票的公司】信息

    /**
     * AJAX 动态获取数据库中开过票的公司信息
     *
     * @param filter    搜索字段
     * @param modelType 16表示开票公司
     * @return 相似项
     */
    @RequestMapping(value = "/getSimilarUnit", method = RequestMethod.POST)
    @ResponseBody
    public Map<Object, Object> getSimilarUnit(String filter, Integer modelType) {

        Map<Object, Object> map = new HashMap<>();

        filter = filter.trim();

        if (StringUtils.isNotBlank(filter)) {
            List<Map<String, String>> dataList = catalogService.getSimilarCatalog(filter, modelType);
            for (Map<String, String> child : dataList) {
                map.put(child.get("similarEn"), child.get("similar"));
            }
        }

        if (map.size() == 0) {
            map.put("0", "未查询到数据");
        }

        return map;
    }

    //endregion

    //region AJAX 动态获取数据库中【公司】信息

    /**
     * AJAX 动态获取数据库中公司信息
     *
     * @param filter 搜索字段
     * @return 相似项
     */
    @RequestMapping(value = "/getSimilarCompany", method = RequestMethod.POST)
    @ResponseBody
    public Map<Object, Object> getSimilarCompany(String filter, Integer companyType) {

        Map<Object, Object> map = new HashMap<>();

        filter = filter.trim();

        if (StringUtils.isNotBlank(filter)) {
            List<Map<String, String>> dataList = companyService.getSimilarCompany(filter, companyType);
            for (Map<String, String> child : dataList) {
                map.put(child.get("id"), child.get("similar"));
            }
        }

        if (map.size() == 0) {
            map.put(0, "未查询到数据");
        }

        return map;
    }

    //endregion

    //region AJAX 动态获取数据库中【物料】信息

    /**
     * AJAX 动态获取数据库中物料信息
     *
     * @param filter 搜索字段
     * @return 相似项
     */
    @RequestMapping(value = "/getSimilarPurchase", method = RequestMethod.POST)
    @ResponseBody
    public Map<Object, Object> getSimilarPurchase(String filter, Integer purchaseType) {

        Map<Object, Object> map = new HashMap<>();

        filter = filter.trim();

        if (StringUtils.isNotBlank(filter)) {
            List<Map<String, String>> dataList = purchaseService.getSimilarPurchase(filter, purchaseType);
            for (Map<String, String> child : dataList) {
                map.put(child.get("id"), child.get("similar"));
            }
        }

        if (map.size() == 0) {
            map.put(0, "未查询到数据");
        }

        return map;
    }

    //endregion

    //region AJAX 动态获取数据库中【会员】信息

    /**
     * AJAX 动态获取数据库中会员信息
     *
     * @param filter   搜索字段
     * @param userType 会员类型：1普通会员 2企业客户 10管理员 20司机
     * @return 相似项
     */
    @RequestMapping(value = "/getSimilarUser", method = RequestMethod.POST)
    @ResponseBody
    public Map<Object, Object> getSimilarUser(String filter, Integer userType) {

        Map<Object, Object> map = new HashMap<>();

        filter = filter.trim();

        if (StringUtils.isNotBlank(filter)) {
            List<Map<String, String>> dataList = userService.getSimilarUser(filter, userType);
            for (Map<String, String> child : dataList) {
                map.put(child.get("id"), child.get("similar"));
            }
        }

        if (map.size() == 0) {
            map.put(0, "未查询到数据");
        }

        return map;
    }

    //获取用户手机号
    @RequestMapping(value = "/getUserMobile", method = RequestMethod.POST)
    @ResponseBody
    public Map<Object, Object> getUserMobile(Integer userId) {
        Map<Object, Object> map = new HashMap<>();
        map.put("mobile", "");

        if (userId != null && userId > 0) {
            TransportUserDTO dto = userService.getFromCacheId(userId);
            if (dto != null) {
                map.put("mobile", dto.getMobile());
            }
        }

        return map;
    }
    //endregion

}
